package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.DeviceListActivity;
import com.eeepay.eeepay_v2.model.DeviceInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.adapter.ABBaseAdapter;
import com.eeepay.v2_library.adapter.ABViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zw on 2016/3/17 0017.
 * 全部、   可下发   两个ListView共用一个adapter
 */
public class DeviceListAdapter extends ABBaseAdapter<DeviceInfo>{

    private boolean isAll = true;
    private List<Integer> positions = new ArrayList<>();//记录选择的Item
    private DeviceListActivity.IssueRecycleListener listener;

    public DeviceListAdapter(Context context, boolean isAll) {
        super(context);
        this.isAll = isAll;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_device_list;
    }

    @Override
    public void convert(final ABViewHolder holder, final DeviceInfo model) {
        Button btn = holder.getView(R.id.btn_may);
        if(isAll){//全部
            btn.setVisibility(View.VISIBLE);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "position = " + holder.getPosition(), Toast.LENGTH_SHORT).show();
                }
            });
            holder.getView(R.id.iv_select).setVisibility(View.GONE);//不需要批量操作
            final int mode;
            if("true".equals(model.getCanIssue())){
                holder.setText(R.id.btn_may, "机具下发");
                mode = Constant.DEVICE_ISSUE;
            }else if("true".equals(model.getCanRecycle())) {
                holder.setText(R.id.btn_may, "机具回收");
                mode = Constant.DEVICE_RECYCLE;
            }else{
                holder.getView(R.id.btn_may).setVisibility(View.GONE);
                mode = -1;
            }
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        listener.btnClick(model.getSN(), mode, 1, isAll);
                    }
                }
            });
        }else{//可下发
            holder.getView(R.id.iv_select).setVisibility(View.VISIBLE);
            btn.setVisibility(View.GONE);
            final ImageView ivselect = holder.getView(R.id.iv_select);
            ivselect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(positions.contains(holder.getPosition())){
                        positions.remove((Integer)holder.getPosition());
                        Log.d("device"," remove: "+holder.getPosition());
                        ivselect.setImageResource(R.drawable.icon_no_tick);
                    }else{
                        positions.add(holder.getPosition());
                        Log.d("device"," add: "+holder.getPosition());
                        ivselect.setImageResource(R.drawable.icon_tick);
                    }
                }
            });
            //add by zhuangzeqin 2017年9月27日09:54:14 修复一个bug 当选择的时候； 下面的控件会复用跟着选择 ；如果记录里有； 则说明已经选择了； 如果没有说明没有选择
            if(positions.contains(holder.getPosition())){
                ivselect.setImageResource(R.drawable.icon_tick);
            }
            else
            {
                ivselect.setImageResource(R.drawable.icon_no_tick);
            }

        }
        //相同的部分
        if("已分配".equals(model.getState())){
            holder.setRightText(R.id.lrv_device_state, model.getState())
                    .setRightTextColor(R.color.green);
        }else if ("已使用".equals(model.getState())){
            holder.setRightText(R.id.lrv_device_state, model.getState())
                    .setRightTextColor(R.color.red);
        }

        holder.setRightText(R.id.lrv_device_sn, model.getSN());
        holder.setRightText(R.id.lrv_terminal, model.getTerminal());
        holder.setRightText(R.id.lrv_merchant_name, model.getMerchantName());
        holder.setRightText(R.id.lrv_agent_name, model.getAgentName());
        holder.setRightText(R.id.lrv_device_state, model.getState());
        //add by zhuangzeqin 2017年6月22日17:56:20 新增活动类型
        holder.setRightText(R.id.lrv_activity_type, model.getActivityName());//如果有多个；"欢乐返,注册返" 则用逗号隔开；暂时写死；后续需要则从实体定义的属性获取
    }

    public List<Integer> getPositions(){
        return this.positions;
    }

    public DeviceInfo getData(int position){
        return list.get(position);
    }

    public void setListener(DeviceListActivity.IssueRecycleListener listener) {
        this.listener = listener;
    }

    /**
     * 获得批量下发时的SN、  字符串格式（1,2,3,4）
     * @return
     */
    public String getBatchIssueSN(){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < positions.size(); i++){
            buffer.append(list.get(i).getSN());
            buffer.append(",");
        }
        if(buffer.length() > 1){
            buffer.deleteCharAt(buffer.length() - 1);
        }else{
            return null;
        }
        return buffer.toString();
    }

}
